package com.ruoyi.hxf.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.hxf.domain.Education;
import com.ruoyi.hxf.mapper.EducationMapper;
import com.ruoyi.hxf.service.IEducationService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 教育摘要 服务层实现
 *
 * @author hxf
 */
@RequiredArgsConstructor
@Service
public class EducationImpl implements IEducationService {

    private final EducationMapper baseMapper;

    @Override
    public TableDataInfo<Education> selectPageEducationList(Education education, PageQuery pageQuery) {
        LambdaQueryWrapper<Education> lqw = new LambdaQueryWrapper<Education>()
            .like(StringUtils.isNotBlank(education.getEducationTitle()), Education::getEducationTitle, education.getEducationTitle())
            .eq(StringUtils.isNotBlank(education.getStatus()), Education::getStatus, education.getStatus())
            .like(StringUtils.isNotBlank(education.getCreateBy()), Education::getCreateBy, education.getCreateBy())
            .like(StringUtils.isNotBlank(education.getUpdateBy()), Education::getUpdateBy, education.getUpdateBy())
            .eq(Education::getIsDelete, 0)
            .last("order by education_id desc");
        Page<Education> page = baseMapper.selectPage(pageQuery.build(), lqw);
        return TableDataInfo.build(page);
    }

    /**
     * 查询教育摘要信息
     *
     * @param educationId 教育摘要ID
     * @return 教育摘要信息
     */
    @Override
    public Education selectEducationById(Long educationId) {
        return baseMapper.selectById(educationId);
    }

    /**
     * 查询教育摘要列表
     *
     * @param education 教育摘要信息
     * @return 教育摘要集合
     */
    @Override
    public List<Education> selectEducationList(Education education) {
        return baseMapper.selectList(new LambdaQueryWrapper<Education>()
            .like(StringUtils.isNotBlank(education.getEducationTitle()), Education::getEducationTitle, education.getEducationTitle())
            .eq(StringUtils.isNotBlank(education.getStatus()), Education::getStatus, education.getStatus())
            .like(StringUtils.isNotBlank(education.getCreateBy()), Education::getCreateBy, education.getCreateBy()));
    }

    /**
     * 新增教育摘要
     *
     * @param education 教育摘要信息
     * @return 结果
     */
    @Override
    public int insertEducation(Education education) {
        return baseMapper.insert(education);
    }

    /**
     * 修改教育摘要
     *
     * @param education 教育摘要信息
     * @return 结果
     */
    @Override
    public int updateEducation(Education education) {
        return baseMapper.updateById(education);
    }

    /**
     * 删除教育摘要对象
     *
     * @param educationId 教育摘要ID
     * @return 结果
     */
    @Override
    public int deleteEducationById(Long educationId) {
        return baseMapper.deleteById(educationId);
    }

    /**
     * 批量删除教育摘要信息
     *
     * @param educationIds 需要删除的教育摘要ID
     * @return 结果
     */
    @Override
    public int deleteEducationByIds(Long[] educationIds) {
        for (Long id : educationIds) {
            Education education = baseMapper.selectById(id);
            education.setIsDelete("1");
            Education update = BeanUtil.toBean(education, Education.class);
            update.setUpdateBy(String.valueOf(LoginHelper.getUsername()));
            baseMapper.updateById(update);
        }
        return 1;
    }

    /**
     * 修改教育摘要信息
     *
     * @param education 教育摘要信息
     * @return
     */
    @Override
    public int myUpdateEducation(Education education) {
        return baseMapper.myUpdateEducation(education);
    }

    /**
     * 查找前三条教育摘要信息
     *
     * @return
     */
    @Override
    public List<Education> SelectCurrEducationLimit() {
        return baseMapper.SelectCurrEducationLimit();
    }

    /**
     * 查找全部的教育摘要信息
     *
     * @return
     */
    @Override
    public List<Education> SelectCurrEducation() {
        return baseMapper.SelectCurrEducation();
    }
}
